/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ucue.ptc.rest;

import edu.ucue.ptc.gco.mdl.Direccion;
import edu.ucue.ptc.gco.ngc.DireccionManagerLocal;
import edu.ucue.ptc.gco.ngc.UsuarioManagerLocal;
import edu.ucue.ptc.gco.util.AtributoInvalidoException;
import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

/**
 * REST Web Service
 *
 * @author Valex
 */
@Path("direcciones/{cliente}/{nombre}/{clave}/{callback}")
@RequestScoped
public class DireccionesResource {

    @Context
    private UriInfo context;
    @Inject
    private UsuarioManagerLocal UsuarioManager;
    @Inject
    private DireccionManagerLocal dirManager;

    /**
     * Creates a new instance of DireccionesResource
     */
    public DireccionesResource() {
    }

    /**
     * Retrieves representation of an instance of
     * edu.ucue.ptc.rest.DireccionesResource
     *
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/json")
    public String getJson(@PathParam("cliente") int idCliente, @PathParam("nombre") String user,
            @PathParam("clave") String pass, @PathParam("callback") String callback) throws AtributoInvalidoException {

        int IdPersona = UsuarioManager.verificarUsuario(user, pass);
        //String a=",\"valor\":\""+verificarUsuario;

        if (IdPersona != -1) {
            
                List<Direccion> list = dirManager.ListarDireccionPorCliente(idCliente,user,pass);

                if (list == null) {
                    return callback + "({\"callback\":\"" + callback + "\",\"errormsg\":\"" + "No tiene direcciones" + "\"})";
                } else {
                    
                    String trama = callback+"({" + "\"callback\":\"" + callback + "\"";
                    for (int i = 0; i < list.size(); i++) {                                               
                        trama=trama+",\"direccion"+i+"\":"+"{\"idDireccion\":\""+list.get(i).getIdDireccion() +"\",\"callePrincipal\":\"" + list.get(i).getCallePrincipal()+"\",\"calleSecundaria\":\""+list.get(i).getCalleSecundaria() +"\",\"latitud\":\""+list.get(i).getLatitud() +"\",\"longitud\":\""+list.get(i).getLongitud() + "\"}";
                    }
                    return trama+  "})";
                }
           
        }
        //buscarEmpleadoPorId
        return "({\"callback\":\"" + callback + "\",\"errormsg\":\"" + "Usuario no registrado" + "\"})";
    }

    /**
     * PUT method for updating or creating an instance of DireccionesResource
     *
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/json")
    public void putJson(String content) {
    }
}
